import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  newsList: {},
  currentNews: {},
  searchNewsCondition: {
    pageSize: 10,
    title: "",
    status: "",
    source: "",
    startTime: "",
    endTime: ""
  }
};

const getters = {
  newsList: state => state.newsList,
  currentNews: state => state.currentNews,
  searchNewsCondition: state => state.searchNewsCondition
};

const actions = {
  getNewsPageS({ commit }, param) {
    return Api.getNewsPage(param).then(ret => {
      commit(types.UPDATE_NEWS_LIST, ret.data.data);
      return ret;
    });
  },
  getNewsS({ commit }, id) {
    return Api.getNews({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_NEWS, ret.data.data);
      return ret;
    });
  },
  addOrUpdateNewsS({ commit }, payload) {
    return Api.addOrUpdateNews({ body: payload }).then(ret => {
      return ret;
    });
  },
  updateNewsS({ commit }, payload) {
    return Api.updateNews(payload).then(ret => {
      return ret;
    });
  },
  recommenNewsS({ commit }, payload) {
    return Api.recommenNews(payload).then(ret => {
      return ret;
    });
  },
  updateTime({ commit }, payload) {
    return Api.updateNews(payload).then(ret => {
      return ret;
    });
  },
  cancelRecommenNewsS({ commit }, payload) {
    return Api.cancelRecommenNews(payload).then(ret => {
      return ret;
    });
  },
  deleteNewsS({ commit }, param) {
    return Api.deleteNews(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_NEWS](state, payload) {
    state.currentNews = {
      type: 0,
      cover1: "",
      cover2: "",
      cover3: "",
      video: "",
      title: "",
      content: "",
      score: 0,
      source: ""
    };
  },
  [types.UPDATE_CURRENT_NEWS](state, payload) {
    console.log("更新当前新闻", payload.content);
    state.currentNews = payload;
  },
  [types.UPDATE_NEWS_LIST](state, payload) {
    state.newsList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
